<{include file="Public/top.html" }>
<style>
    .waveset{width: 49%;float: left;}
    .waveset .form-box{height: 250px}
    .param{height:3rem;}
    </style>
	</head>
	<body>
		<div id="warpper">
            <div class="content">
                <div class="location">自定义表单</div>
                <{include file="./nav.html" }>

                <form action="<{u(update)}>" class="ui-form" id="addManagerForm" method="post" >
                <div class="topic"> 表单设置</div>
                <div class="form-box border0 border-box" id="setdiv" >
                        <input type="hidden" name="id" value="<{$id|default:0}>" />
                        <div class="ui-form-item">
                            <label class="ui-label">活动名称</label>
                            <input type="text" class="ui-text"  name="title" value="<{$title}>" required/>
                        </div>

                        <div class="ui-form-item">
                            <label class="ui-label">备注说明</label>
                            <textarea  class="ui-textarea"  name="mem"  required><{$mem}></textarea>
                        </div>
                    <div class="ui-form-item">
                        <label class="ui-label">是否必须关注</label>
                        <input type="radio"  name="mustFollow" value="0" <{if $mustFollow eq 0}> checked <{/if}> ><label >否</label>
                        <input type="radio"  name="mustFollow" value="1" <{if $mustFollow eq 1}> checked <{/if}> ><label >是</label>
                    </div>

                </div>
                    <{foreach from=$params key=key item=item}>
                    <div class="content waveset">
                        <div class="topic">字段<i class="wave"><{$key}></i> &nbsp;&nbsp;&nbsp;&nbsp;<a href="javascript:void(0)" onclick="removeWave(this)" class="del-button">删除</a></div>
                        <div class="form-box border0 border-box">
                            <div class="ui-form-item">
                                <label class="ui-label">字段名称</label>
                                <input type="phone" class="ui-text"  name="fname[]" value="<{$item.fname}>" required/>
                            </div>
                            <div class="ui-form-item">
                                <label class="ui-label">字段类型</label>
                                <select  class="ui-select" name="ftype[]" onchange="changeType(this)">
                                    <{html_options options=$fieldKv selected=$item.ftype}>
                                </select>
                            </div>
                            <{if $item.ftype eq $FIELD_SINGLE or $item.ftype eq $FIELD_MULTI}>
                                <div class="ui-form-item">
                                    <label class="ui-label">备选参数</label>
                                    <textarea  class='ui-text param' name='fparam[]'  required><{$item.fparam}></textarea>
                                </div>
                            <{else}>
                                <div class="ui-form-item" style="display: none">
                                    <label class="ui-label">备选参数</label>
                                    <input class="ui-text param" type="hidden" name="fparam[]">
                                </div>
                            <{/if}>

                            <div class="ui-form-item">
                                <label class="ui-label">是否必填</label>
                                <input type="radio"  name="must[<{$key}>]" value="0" <{if $item.must eq 0}> checked <{/if}>><label >否</label>
                                <input type="radio"  name="must[<{$key}>]" value="1" <{if $item.must eq 1}> checked <{/if}> ><label >是</label>
                            </div>
                            <div class="ui-form-item">
                                <label class="ui-label">字段补充说明</label>
                                <input type="phone" class="ui-text"  name="append[]" value="<{$item.append}>" />
                            </div>
                        </div>
                    </div>
                    <{/foreach}>

                    <div id="clear" style="clear: both"></div>
                    <{if $obj.id eq 0 }>
                        <div class="integral_btn-group">
                            <button type="button"  class="ui-button " id="moreWave">增加字段</button>
                            <input type="submit" value="提交" class="ui-button"/>
                            <input type="reset"  value="清空" class="ui-button fade-o-butn"/>
                        </div>
                    <{else}>
                        <{if $canEdit}>
                            <div class="integral_btn-group">
                                <button type="button"  class="ui-button " id="moreWave">增加字段</button>
                                <input type="submit" value="修改" class="ui-button"/>
                            </div>
                        <{/if}>
                    <{/if}>

                </form>
            </div>
		</div>

        <div class="content waveset" id="template" style="display: none">
            <div class="topic">字段<i class="wave">1</i> &nbsp;&nbsp;&nbsp;&nbsp;<a href="javascript:void(0)" onclick="removeWave(this)" class="del-button">删除</a></div>
            <div class="form-box border0 border-box">
                <div class="ui-form-item">
                    <label class="ui-label">字段名称</label>
                    <input type="phone" class="ui-text"  name="fname[]"  required/>
                </div>

                <div class="ui-form-item" >
                    <label class="ui-label">字段类型</label>
                    <select  class="ui-select" name="ftype[]" onchange="changeType(this)">
                        <{html_options options=$fieldKv}>
                    </select>
                </div>

                <div class="ui-form-item" style="display: none">
                    <label class="ui-label">备选参数</label>
                    <input class="ui-text param" type="hidden" name="fparam[]">
                </div>

                <div class="ui-form-item">
                    <label class="ui-label">是否必填</label>
                    <input type="radio"  name="must[]" value="0" ><label >否</label>
                    <input type="radio"  name="must[]" value="1" ><label >是</label>
                </div>
                <div class="ui-form-item">
                    <label class="ui-label">字段补充说明</label>
                    <input type="phone" class="ui-text"  name="append[]"  />
                </div>
            </div>
        </div>



        <{include file="Public/foot.html" }>
		<script>
            var num=99;
            $(document).ready(function(){
                resetWaveNum();
                $('#moreWave').click(function(e){
                    num++;
                    var $wave=$('#template').clone().removeAttr('id').insertBefore('#clear').show();
                    resetWaveNum();
                });

                $("form").submit(function () {
                    layer.msg("正在提交数据...",{time:999});
                    $.post("<{U('update')}>",$(this).serialize(), function(data){
                        if(data.status==0){
                             layer.msg(data.info);
                        }else{
                            layer.msg(data.info);
                            location.href="<{U('index')}>";
                        }
                    }, 'json');
                    return false;
                });
            });


            function resetWaveNum(){
                $('.waveset').each(function(i){
                        var $obj=$(this);
                        $obj.find('.wave').text(i+1);
                        $obj.find('input[type="radio"]').attr('name','must['+i+']');
                    }
                );
            }

            function removeWave(obj){
                $(obj).closest(".waveset").remove();
                resetWaveNum();
            }

            function changeType(obj){
                var val=obj.value;
                switch(val.toString()){
                    case '<{$FIELD_SINGLE}>':
                    case '<{$FIELD_MULTI}>':
                        $(obj).parent().next().show().find('.param').replaceWith("<textarea  class='ui-text param' name='fparam[]'  required></textarea>");
                        break;
                    default:
                        $(obj).parent().next().hide().find('.param').replaceWith("<input class='param' type='hidden' name='fparam[]'>");
                        break;
                }
            }

        </script>
	</body>
</html>
